//
//  StoreStarView.m
//  WTM
//
//  Created by scott on 2017/8/7.
//  Copyright © 2017年 lxj. All rights reserved.
//

#import "StarView.h"

@implementation StarView
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        for (int i = 0; i < 5; i++) {
            UIButton *button = [[UIButton alloc] init];
//            button.frame = CGRectMake( frame.size.height * i, 0, frame.size.height,  frame.size.height);
            button.userInteractionEnabled = NO;
            [button setImage:[UIImage imageNamed:@"ic_collection-"] forState:UIControlStateNormal];
            button.tag = i + 100;
            [self addSubview:button];
        }
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
//    ic_collection
//    ic_score_3
//    ic_collectioned
    if (self = [super initWithFrame:frame]) {
        for (int i = 0; i < 5; i++) {
            UIButton *button = [[UIButton alloc] init];
            button.frame = CGRectMake( frame.size.height * i, 0, frame.size.height,  frame.size.height);
            button.userInteractionEnabled = NO;
            [button setImage:[UIImage imageNamed:@"ic_collection-"] forState:UIControlStateNormal];
            button.tag = i + 100;
            [self addSubview:button];
        }
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat padding = (self.frame.size.width - self.frame.size.height * 5) / 4.0;

    for (int i = 0; i < 5; i++) {
        UIButton *button =  [self viewWithTag:i + 100];
        button.frame = CGRectMake((self.frame.size.height + padding) * i, 0, self.frame.size.height,  self.frame.size.height);
    }
}
-(void)setCount:(float)count{
    _count = count;
    if (_count > 5.0) {
        _count = 5.0;
    }
    if (_count < 0) {
        _count = 0;
    }

    for (int i = 0; i < 5; i++) {
        UIButton *button = [self viewWithTag:i + 100];
        if (_count - i >= 1) {//全星
            [button setImage:[UIImage imageNamed:@"ic_collectioned"] forState:UIControlStateNormal];
        }else if (_count - i > 0){//半星
            [button setImage:[UIImage imageNamed:@"ic_score_3"] forState:UIControlStateNormal];
        }else{//空心
            [button setImage:[UIImage imageNamed:@"ic_collection"] forState:UIControlStateNormal];
        }
    }
}

@end

